package com.og.managedbean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.log4j.Logger;

import com.og.backingbean.Company;
import com.og.backingbean.Person;
import com.og.dao.CRUDEntityDAO;

/**
 * @author Renaud DIANA
 *
 */
@Named
@SessionScoped
public class CompanyBean implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	// Log4j	 
	private static final Logger	log	= Logger.getLogger(CompanyBean.class);
				
    /* Dependency injection */
	// Model
    @Inject
    private CRUDEntityDAO<Company> companyDAO;
    @Inject
    private CRUDEntityDAO<Person> personDAO;
    // BackingBean    
    @Inject
    private Company companyView;   
    @Inject
    private Company companyUpdate;  
    @Inject
    private Company companyNew;  
    @Inject
    private Person person;
    @Inject
    private ListBean list;
        
    /* -------------------- */
    
	/**
	 * Default constructor
	 */
	public CompanyBean() {
		// TODO Auto-generated constructor stub
	}
	
	public String create() {
		
		// Add FK
		person.setCompany(companyNew);
				
		// Set List
		List<Person> listPersons = new ArrayList<Person>();
		listPersons.add(person);		
		companyNew.setListPersons(listPersons);
		
		// Call model				
		companyDAO.createEntity(companyNew);
		
		// Generate new list
		list.findAllCompany();
		list.findAllPerson();
		
		// Clear form
		
		companyNew = new Company();
										
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Create company Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}
	
	public String update() {
						
		// Call model				
		companyDAO.updateEntity(companyUpdate);
		
		// Generate new list
		list.findAllCompany();
		list.findAllPerson();
		
		// Toggle view
		companyView = companyUpdate;
										
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Update company Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}
	
	public String delete() {
				
		// Set List	
		Map<String, String> map = new HashMap<String, String>();
		map.put("idCompany", companyView.getIdCompany());
		List<Person> tempList = personDAO.findByNamedQuery("Person.findByCompany", person, map);			
		companyView.setListPersons(tempList);
		
		// Call model				
		companyDAO.deleteEntity(companyView);
		
		// Generate new list
		list.findAllCompany();
		list.findAllPerson();
				
		// Clear form
		companyView = new Company();
						
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Delete company Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}
	
	public String findOne() {
				
		// Call model				
		companyView = companyDAO.findOneEntity(companyView, companyView.getIdCompany());
		
		// Populate person(s)		
		try {
			person = companyView.getListPersons().get(0);
		} catch (NullPointerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
					
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Find company Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}
	
	public String toggleUpdate() {
		
		companyUpdate = companyView;
		
		return "";
	}
	
	/* 
	// For wizard
	public String onFlowProcess(FlowEvent event) {  
        
        return event.getNewStep();         
    }  
	*/	
	
	// Getters and Setters
		
	public Company getCompanyView() {
		return companyView;
	}

	public void setCompanyView(Company companyView) {
		this.companyView = companyView;
	}

	public Company getCompanyUpdate() {
		return companyUpdate;
	}

	public void setCompanyUpdate(Company companyUpdate) {
		this.companyUpdate = companyUpdate;
	}

	public Company getCompanyNew() {
		return companyNew;
	}

	public void setCompanyNew(Company companyNew) {
		this.companyNew = companyNew;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

}
